/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.templates;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.L1DatabaseFactory;
import com.lineage.server.utils.SQLUtil;

/**
 * Class <code>L1DoorGfx</code> 数据库模版:门的图形(Gfx).
 * 
 * @author jrwz
 * @version 2012-6-16上午06:03:40
 * @see com.lineage.server.templates
 * @since JDK1.7
 */
public final class L1DoorGfx {

    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(L1DoorGfx.class
            .getName());

    /**
     * 返回 从door_gfxs表指定的主键gfxid的一行.
     * 此方法始终返回表内的最新数据
     * 
     * @param gfxId
     *            门的图形编号
     * @return 没有找到则返回 null
     */
    public static L1DoorGfx findByGfxId(final int gfxId) {
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = //
            con.prepareStatement("SELECT * FROM door_gfxs WHERE gfxid = ?");
            pstm.setInt(1, gfxId);
            rs = pstm.executeQuery();
            if (!rs.next()) {
                return null;
            }
            final int id = rs.getInt("gfxid");
            final int dir = rs.getInt("direction");
            final int rEdge = rs.getInt("right_edge_offset");
            final int lEdge = rs.getInt("left_edge_offset");
            return new L1DoorGfx(id, dir, rEdge, lEdge);

        } catch (final SQLException e) {
            LOG.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(rs);
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }
        return null;
    }

    /** 门的图形编号. */
    private final int gfxIdDoor;
    /** 门的方向. */
    private final int directionDoor;
    /** 向右开. */
    private final int rightEdgeOffsetDoor;
    /** 向左开. */
    private final int leftEdgeOffsetDoor;

    /**
     * 数据库模版:门的图形(Gfx).
     * 
     * @param gfxId
     *            门的图形编号
     * @param direction
     *            门的方向
     * @param rightEdgeOffset
     *            向右开
     * @param leftEdgeOffset
     *            向左开
     */
    private L1DoorGfx(final int gfxId, final int direction,
            final int rightEdgeOffset, final int leftEdgeOffset) {
        this.gfxIdDoor = gfxId;
        this.directionDoor = direction;
        this.rightEdgeOffsetDoor = rightEdgeOffset;
        this.leftEdgeOffsetDoor = leftEdgeOffset;
    }

    /**
     * 取得门的方向.
     * 
     * @return 门的方向
     */
    public int getDirection() {
        return this.directionDoor;
    }

    /**
     * 取得门的图形编号.
     * 
     * @return 门的图形编号
     */
    public int getGfxId() {
        return this.gfxIdDoor;
    }

    /**
     * 取得打开方向:向左开.
     * 
     * @return 向左开
     */
    public int getLeftEdgeOffset() {
        return this.leftEdgeOffsetDoor;
    }

    /**
     * 取得打开方向:向右开.
     * 
     * @return 向右开
     */
    public int getRightEdgeOffset() {
        return this.rightEdgeOffsetDoor;
    }
}
